/**
 * Created by zengy on 2017/6/12.
 */
angular.module('mainApp.evaluate',['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state({
                name:'evaluate/evaluateList',
                url:'/evaluate/evaluateList',
                templateUrl:'views/evaluate/evaluate_list.html?v='+new Date().getMinutes(),
                controller:'evaluateCtrl'
            })
    })
    .controller('evaluateCtrl',function ($scope) {
        $scope.$emit('arg',8)
        var date=new Date();
        $scope.evaluateModel={
            pageNo:1,   //查询页数
            pageSize:10, //每页数条数
            startTimeRange: date.getFullYear() + "-" + (date.getMonth()) + "-" + date.getDate(),
            endTimeRange: date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate(),
            userName:'',
            orderNo:''
        }
        $('.date-picker').datepicker({
            autoclose: true,
            changeMonth: true,
            language : 'zh-CN',
            format : 'yyyy-mm-dd',
            showOptions: {direction: 'up'}
        })
        $scope.tagsModel={};
        $scope.modifyModel=[];
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getCommentList(page)
                }
            });
        };
        $scope.getCommentList=function (page) {
            if(page){
                $scope.evaluateModel.pageNo=page;
                $scope.evaluateModel.pageSize=10;
            }
            if(!$scope.evaluateModel.startTimeRange && $scope.evaluateModel.endTimeRange){
                layer.msg('时间筛选不不符合规则！');
                return false;
            }
            if(parseInt(new Date($scope.evaluateModel.startTimeRange).getTime()/1000) > parseInt(new Date($scope.evaluateModel.endTimeRange).getTime()/1000)){
                layer.msg('开始时间不能大于结束时间！');
                return false;
            }
            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            new HttpRequest().getCommentList($scope.evaluateModel, function (res) {
                layer.closeAll();
                if (res.code == '000') {
                    $scope.evaluateList = res.data.list;
                    $scope.$apply();
                    if ($scope.evaluateList.length > 0) {
                        $scope.pagination($scope.evaluateModel.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg(res.msg)
                }
            })
        }
        $scope.getCommentList($scope.evaluateModel.pageNo)

        //通话
        $scope.conversation=function (event) {
            var _this=$(event.target);
           // var userId=_this.data('userId');
            var cellPhone=_this.attr('data-usercellphone');
            var username=_this.attr('data-username');
            console.log(username)
            var index = layer.confirm('与'+username+'联系<br />联系电话'+cellPhone, {
                title: '通话',
                btn: ['确定', '取消'],//按钮
            }, function () {
                layer.close(index)
                new HttpRequest().customServiceCalling({OrderNo:'170612100011',toMobile:cellPhone},function (res) {
                    if(res.code !='000' && res.data !='200'){
                        layer.msg('操作失败！');
                    }else{
                        layer.msg('通话中。。。');
                    }
                })
            }, function () {
                layer.close(index)
            })
        }
        //评论审核
        $scope.verify=function (event) {
            var _this=$(event.target);
            var datas=_this.data('detail');
            var tags=datas.tags.split(" ") ;
            $scope.tags=tags;
            var isHidden='0';
            var doctorUserId=datas.doctorUserId;
            var commentTagIds=datas.commentTagIds
            var serviceCommentId=datas.serviceCommentRowId;
            $scope.tagsModel={
                isHidden:isHidden,
                doctorUserId:doctorUserId,
                commentTagIds:commentTagIds,
                serviceCommentId:serviceCommentId
            }
        }
        $scope.commit=function () {
            new HttpRequest().checkComment($scope.tagsModel,function (res) {
                if(res.code=='000'){
                    location.reload();
                }else{
                    layer.msg('操作失败')
                }
            })
        }
        //评论导出
        $scope.exportExcel=function () {
            delete $scope.evaluateModel.pageSize;
            delete $scope.evaluateModel.pageNo;
            if(!$scope.evaluateModel.startTimeRange && $scope.evaluateModel.endTimeRange){
                layer.msg('时间筛选不不符合规则！');
                return false;
            }
            if(parseInt(new Date($scope.evaluateModel.startTimeRange).getTime()/1000) > parseInt(new Date($scope.evaluateModel.endTimeRange).getTime()/1000)){
                layer.msg('开始时间不能大于结束时间！');
                return false;
            }
            new HttpRequest().exportCommentManagerExcel($scope.evaluateModel,function (res) {
                if(res.code=='000'){
                    location.href=baseUrl+res.data;
                }else{
                    layer.msg('导出失败！');
                }
            })
        }
        //获取标签列表
        $scope.getTagsList=function () {
            new HttpRequest().getTags(function (res) {
                if(res.code == '000'){
                    $scope.tagsList=res.data;
                    console.log($scope.tagsList)
                    $scope.$apply();
                }
            })
        }
        $scope.getTagsList();
        //编辑便签
        $('body').on('change','input[name="tags"]',function () {
            var _this=$(this);
            var datas=JSON.parse(_this.attr('data-detail'));
            var name=_this.val();
            var newobj={
                rowId:datas.rowId,
                name:name,
                ratings:datas.ratings,
                isDeleted:datas.isDeleted
            }
            $scope.modifyModel.push(JSON.stringify(newobj));
        })

        $scope.edtTags=function () {
            var argStr='['+$scope.modifyModel.join(",")+']';
            //console.log(argStr)
            if($scope.modifyModel){
                new HttpRequest().editTags({wxCommentTag:argStr},function (res) {
                    if(res.code=='000'){
                        location.reload()
                    }else{
                        layer.msg('操作失败！');
                    }
                })
            }

        }


    })
